package com.orangelife.service.propFeeApartment.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeApartment.IPropFeeApartmentService;
import com.orangelife.service.propFeeApartment.model.PropFeeApartmentDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeApartment类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-06-18 12:26:04
 * ==============================================================
 */
@Service("propFeeApartmentService")
public class PropFeeApartmentServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeApartmentService {
	private static Logger logger = Logger
			.getLogger(PropFeeApartmentServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeApartmentServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeApartment(PropFeeApartmentDto propFeeApartment)
			throws AppBizException {

		Assert.notNull(propFeeApartment);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeApartment.getFeeArea())) {
			if (propFeeApartment.getFeeArea().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "计费面积长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getFloor())) {
			if (propFeeApartment.getFloor().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "楼层长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getApartmentNum())) {
			if (propFeeApartment.getApartmentNum().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "单元房门牌号长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getApartmentType())) {
			if (propFeeApartment.getApartmentType().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"房屋类型 （0单元房  1车位  别墅2  商铺=3 ）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getIsOccupy())) {
			if (propFeeApartment.getIsOccupy().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"是否入住（0未入住，1已入住）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getIsDeliver())) {
			if (propFeeApartment.getIsDeliver().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"是否交房(0未交房，1 已交房)长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeApartment.getApartmentID())) {

			propFeeApartment.setApartmentID(IdUtil.getuuId());
		}
		propFeeApartment.setItemStat(TypeConst.FLAG_ON);
		propFeeApartment.setTscreate(new Date());
		propFeeApartment.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeApartmentMapper.insertSelective", propFeeApartment);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeApartment(PropFeeApartmentDto propFeeApartment)
			throws AppBizException {

		Assert.notNull(propFeeApartment);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeApartment.getApartmentID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeApartmentDto oldPropFeeApartment = this
				.findPropFeeApartmentByID(propFeeApartment.getApartmentID());

		if (!StringUtils.isBlank(propFeeApartment.getFeeArea())) {
			if (propFeeApartment.getFeeArea().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "计费面积长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getFloor())) {
			if (propFeeApartment.getFloor().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "楼层长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getApartmentNum())) {
			if (propFeeApartment.getApartmentNum().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "单元房门牌号长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getApartmentType())) {
			if (propFeeApartment.getApartmentType().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"房屋类型 （0单元房  1车位  别墅2  商铺=3 ）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getIsOccupy())) {
			if (propFeeApartment.getIsOccupy().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"是否入住（0未入住，1已入住）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeApartment.getIsDeliver())) {
			if (propFeeApartment.getIsDeliver().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"是否交房(0未交房，1 已交房)长度超出");
			}
		}

		if (oldPropFeeApartment == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeApartment.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeApartmentMapper.updateByPrimaryKeySelective",
				propFeeApartment);
	}

	@Override
	public PropFeeApartmentDto findPropFeeApartmentByID(
			String propFeeApartmentID) throws AppBizException {
		if (StringUtils.isBlank(propFeeApartmentID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeApartmentDto oldPropFeeApartment = this.getSqlSession()
				.selectOne("PropFeeApartmentSearchMapper.selectByPrimaryKey",
						propFeeApartmentID);
		if (oldPropFeeApartment == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeApartment.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeApartment;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeApartmentByLogic(String propFeeApartmentID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeApartmentID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeApartmentDto oldPropFeeApartment = this
				.findPropFeeApartmentByID(propFeeApartmentID);

		oldPropFeeApartment.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeApartment.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeApartmentMapper.updateByPrimaryKeySelective",
				oldPropFeeApartment);
	}

	@Override
	public List<Map<String, Object>> findPropFeeApartmentByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeApartmentSearchMapper.findAllPropFeeApartment", map);
		return list;
	}

	@Override
	public List<PropFeeApartmentDto> getPropFeeApartmentByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeApartmentDto> list = this.getSqlSession().selectList(
				"PropFeeApartmentSearchMapper.getAllPropFeeApartmentDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeApartmentByRelevantID(
			String apartmentID, String sourceID, String buildingID,
			String unitID, String billTypeID, String apartmentType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("apartmentID", apartmentID);
		map.put("sourceID", sourceID);
		map.put("buildingID", buildingID);
		map.put("unitID", unitID);
		map.put("billTypeID", billTypeID);
		map.put("apartmentType", apartmentType);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeApartmentSearchMapper.selectPropFeeApartmentDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeApartmentDto> findPropFeeApartmentByRelevantID(
			String apartmentID, String sourceID, String buildingID,
			String unitID, String billTypeID, String apartmentType,
			String apartmentNumb) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("apartmentID", apartmentID);
		map.put("sourceID", sourceID);
		map.put("buildingID", buildingID);
		map.put("unitID", unitID);
		map.put("billTypeID", billTypeID);
		map.put("apartmentType", apartmentType);
		map.put("apartmentNum", apartmentNumb);

		List<PropFeeApartmentDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeApartmentSearchMapper.findPropFeeApartmentDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeApartmentDto> bauthfindPropFeeApartment(
			List<String> propFeeApartmentIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeApartmentDto> list = this.getSqlSession().selectList(
				"PropFeeApartmentSearchMapper.batchFindPropFeeApartment",
				propFeeApartmentIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeApartmentByMap(
			Map<String, Object> propFeeApartment) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeApartmentSearchMapper.batchFindPropFeeApartmentByMap",
				propFeeApartment);
		return list;
	}

	@Override
	public int addPropFeeApartment(List<PropFeeApartmentDto> propFeeApartment)
			throws AppBizException {
		if (propFeeApartment != null && propFeeApartment.size() > 0) {
			return this.getSqlSession()
					.insert("PropFeeApartmentBauthMapper.bauthInsert",
							propFeeApartment);
		}
		return 1;
	}

	@Override
	public List<Map<String, Object>> getPropFeeApartmentByRelevantID(
			Map<String, Object> map) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeApartmentSearchMapper.getPropFeeApartmentByRelevantID",
				map);
		return list;
	}

	@Override
	public List<Map<String, Object>> getHouseInfo(
			Map<String, Object> propFeeApartment) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeApartmentSearchMapper.getHouseInfo", propFeeApartment);
		return PropertyUtils.turnToEntity(list);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int bauthDeletePropFeeApartment(List<String> apartmentIDs)
			throws AppBizException {
		return this.getSqlSession().update(
				"PropFeeApartmentBauthMapper.bauthDeleteLg", apartmentIDs);
	}

}